package tech.popsoft.cip.manage.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.popsoft.cip.manage.service.ApiAppService;
import tech.popsoft.cip.manage.service.ApiMessageSubscriptionService;
import tech.popsoft.cip.manage.service.ApiMessageTopicService;
import tech.popsoft.platform.common.base.BaseServiceImpl;
import tech.popsoft.cip.manage.entity.ApiApp;
import tech.popsoft.cip.manage.entity.ApiMessageSubscription;
import tech.popsoft.cip.manage.mapper.ApiMessageSubscriptionMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息订阅 服务实现类
 * @author wqliu
 * @date 2021-08-21
 *
 */
@Service
public class ApiMessageSubscriptionServiceImpl extends BaseServiceImpl<ApiMessageSubscriptionMapper, ApiMessageSubscription> implements ApiMessageSubscriptionService {

    @Autowired
    private ApiAppService apiAppService;

    @Autowired
    private ApiMessageTopicService apiMessageTopicService;

    @Override
    public ApiMessageSubscription init() {
        ApiMessageSubscription entity=new ApiMessageSubscription();

        return entity;
    }

    @Override
    public List<String> getSubscriberList(String code) {
        //获取消息主题标识
        String topicId = apiMessageTopicService.getIdByCode(code);
        //获取订阅者列表
        List<ApiMessageSubscription> idList = this.lambdaQuery().eq(ApiMessageSubscription::getMessageTopicId,
                topicId)
                .list();
        if(CollectionUtils.isNotEmpty(idList)){
            List<String> appIdList=idList.stream().map(x->x.getAppId()).collect(Collectors.toList());
            List<String> appCodeList = apiAppService.lambdaQuery().in(ApiApp::getId, appIdList).list()
                    .stream().map(x -> x.getCode()).collect(Collectors.toList());
            return appCodeList;

        }else{
            return null;
        }

    }
}
